using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using YayiMgr.Ability.CommonApp;
using YayiMgr.Ability.Docking.Commom;
using YayiMgr.Ability.Docking.SuppliersApp;
using YayiMgr.Ability.Docking.SuppliersApp.Dto;
using YayiMgr.Domain.BasicData;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;
using Youshow.Ace.ModelMapping;

namespace YayiMgr.Ability.SuppliersApp
{
    public class SuppliersServicer: AbilityServicer, ISuppliersServicer
    {
        public IRepository<Suppliers> RepoSuppliers { get; set; }

        public async Task<QueryListDto<SuppliersDto>> GetAllSuppliers()
        {
            List<Suppliers> suppliers = await RepoSuppliers.GetListAsync();
            List<SuppliersDto> items = ModelMapper.Map<List<Suppliers>, List<SuppliersDto>>(suppliers);
            return QueryListDto<SuppliersDto>.Create(items, items.Count, 1, items.Count);
        }

        public async Task<QueryListDto<SuppliersDto>> GetSuppliers(SuppliersQueryDto dto)
        {
            IQueryable<Suppliers> suppliers = RepoSuppliers.AsQueryable().Where(s=> s.CompanyId == dto.CompanyId);
            if(dto.IsProxy != null){
                suppliers = suppliers.Where(s=> s.IsProxy == dto.IsProxy);
            }
            if(dto.CreditLimit!=null)
            {
                suppliers = suppliers.Where(s=> s.CreditLimit == dto.CreditLimit);
            }
            if(dto.Rating!=null)
            {
                suppliers = suppliers.Where(s=> s.Rating == dto.Rating);
            }
            if(string.IsNullOrWhiteSpace(dto.SupplierName))
            {
                suppliers = suppliers.Where(s=> s.SupplierName.Contains(dto.SupplierName));
            }
            if(string.IsNullOrWhiteSpace(dto.ContactName))
            {
                suppliers = suppliers.Where(s=> s.ContactName.Contains(dto.ContactName));
            }
            if(string.IsNullOrWhiteSpace(dto.Address))
            {
                suppliers = suppliers.Where(s=> s.Address.Contains(dto.Address));
            }
            if(string.IsNullOrWhiteSpace(dto.Phone))
            {
                suppliers = suppliers.Where(s=> s.Phone.Contains(dto.Phone));
            }
            List<Suppliers> supplierList = await suppliers.OrderBy(s => s.SupplierName)
                                        .Skip((dto.PageIndex-1)*dto.PageSize).Take(dto.PageSize).ToListAsync();
            List<SuppliersDto> items = ModelMapper.Map<List<Suppliers>, List<SuppliersDto>>(supplierList);
            return QueryListDto<SuppliersDto>.Create(items, items.Count, 1, items.Count);
        }

        public async Task<ResponseDto<SuppliersDto>> GetSupplierById(Guid id)
        {
            Suppliers suppliers = await RepoSuppliers.GetAsync(id);
            if(suppliers == null)
            {
                return ResponseDto<SuppliersDto>.CreateFail("无数据");
            }
            SuppliersDto dto = ModelMapper.Map<Suppliers, SuppliersDto>(suppliers);
            return ResponseDto<SuppliersDto>.CreateSuccess(dto);
        }

        public async Task<ResponseDto<SuppliersDto>> AddSuppliers(SuppliersCreateDto dto)
        {
            Suppliers data = ModelMapper.Map<SuppliersCreateDto, Suppliers>(dto);
            data = await RepoSuppliers.InsertAsync(data);
            SuppliersDto dto2 = ModelMapper.Map<Suppliers, SuppliersDto>(data);
            return ResponseDto<SuppliersDto>.CreateSuccess(dto2);
        }
        
        public async Task<ResponseDto<SuppliersDto>> UpdateSuppliers(Guid id, SuppliersUpdateDto dto)
        {
            Suppliers suppliers = await RepoSuppliers.GetAsync(id);
            if(suppliers == null)
            {
                return ResponseDto<SuppliersDto>.CreateFail("无数据");
            }
            suppliers.UpdateEntity(ModelMapper.Map<SuppliersUpdateDto,Suppliers>(dto));
            ModelMapper.Map<Suppliers, SuppliersDto>(suppliers);
                return ResponseDto<SuppliersDto>.CreateFail("无数据");
        }
        
        public async Task<ResponseDto<Object>> DeleteSuppliers(Guid id)
        {
            Suppliers suppliers = await RepoSuppliers.GetAsync(id);
            if(suppliers == null)
            {
                return ResponseDto<Object>.CreateFail("无数据");
            }
            await RepoSuppliers.DeleteAsync(suppliers);
            return ResponseDto<Object>.CreateSuccess(new {});
        }
    }
}